<template>
  <div>
    <!-- 搜索页面头部 -->
    <div class="search-header">
      <!-- 后退按钮 -->
      <van-icon
        name="arrow-left"
        color="white"
        size="0.48rem"
        class="goback"
        @click="$router.back()"
      />
      <!-- 搜索组件 -->
      <van-search
        v-fous
        v-model="kw"
        placeholder="请输入搜索关键词"
        background="#007BFF"
        shape="round"
        @input="inputFn"
        @search="searchFn"
      />
    </div>
    <!-- 搜索建议列表 -->
    <div class="sugg-list" v-if="kw.length">
      <div
        class="sugg-item"
        v-html="lingFn(item, kw)"
        v-for="(item, index) in searchList" :key="index"
        @click="searchFn(item)"
      ></div>
    </div>
    <!-- 搜索历史 -->
    <div class="search-history" v-else>
      <!-- 标题 -->
      <van-cell title="搜索历史">
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template #right-icon>
          <van-icon name="delete" class="search-icon" @click="history = []"/>
        </template>
      </van-cell>

      <!-- 历史列表 -->
      <div class="history-list" v-for="(items,index) in history" :key="index" @click="searchFn(items)">
        <span class="history-item">{{items}}</span>
      </div>
    </div>
  </div>
</template>

<script>
/* eslint-disable */
import { SuggestionAPI } from "@/api";

export default {
  data() {
    return {
      kw: "", // 搜索关键字
      timer: null,
      searchList: [],
      history: JSON.parse(localStorage.getItem('his')) || ["API", "java", "css", "前端", "后台接口", "python"], // 搜索历史
    };
  },
  watch: {
    history() { 
      localStorage.setItem('his', JSON.stringify(this.history))
    }
  },
  methods: {
    inputFn() {
      clearTimeout(this.timer);
      this.timer = setTimeout(async () => {
        if (this.kw.length === 0) return;
        const res = await SuggestionAPI({ q: this.kw });
        this.searchList = res.data.data.options;
      }, 1000);
    },
    // old搜索出来的每项，target搜索框输入的文字
    lingFn(old, target) {
      // 这里没法使用正则的字面量创建，因为我们需要使用变量传递
      const res = new RegExp(target, "ig");
      console.dir(res);
      return old.replace(res, (match) => {
        return `<span style="color:red">${match}</span>`;
      });
    },
    searchFn(item, items) {
      this.history.push(item || this.kw || items)
      this.history = Array.from(new Set(this.history))
      setTimeout(() => { 
        this.$router.push(`/search/${item || this.kw || items}`)
      })
    },
  },
};
</script>

<style scoped lang="less">
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: #007bff;
  overflow: hidden;
  /*后退按钮*/
  .goback {
    padding-left: 14px;
  }
  /*搜索组件*/
  .van-search {
    flex: 1;
  }
}
/*搜索建议列表样式 */
.sugg-list {
  .sugg-item {
    padding: 0 15px;
    border-bottom: 1px solid #f8f8f8;
    font-size: 14px;
    line-height: 50px;
    // 实现省略号的三行代码
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-align: left;
  }
}
/**搜索历史 */
.search-icon {
  font-size: 16px;
  line-height: inherit;
}

.history-list {
  padding: 0 10px;
  .history-item {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>
